package fr.labri.macchiato.client.preferences;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.CheckboxItem;
import com.smartgwt.client.widgets.layout.HLayout;

import fr.labri.macchiato.client.Macchiato;
import fr.labri.macchiato.framework.business.service.Services;

/**
 * A window which asks the user if he want to configure the application if there
 * is no saved settings
 * 
 * @author Matthieu FOUCAULT
 * 
 */
public class PreferencesPrompt extends Window {

	private static final String TITLE = "Sauvegarder ce choix et ne plus me redemander";
	/**
	 * Name of the "Ask Again" checkbox Used to retrieve the value in the main
	 * panel
	 */
	public static final String ASK_AGAIN = "askAgain";
	private static final String PROMPT_MESSAGE = "Vous n'avez aucune préférence utilisateur enregistrée, voulez-vous configurer l'application?";
	private static final String YES = "Oui";
	private static final String NO = "Non, je veux garder la configuration par défaut";

	private HLayout buttonsLayout, saveLayout;

	private Label promptLabel;
	private IButton yesButton;
	private IButton noButton;

	private DynamicForm saveForm;
	private CheckboxItem dontAskAgainCheckBox;

	/**
	 * Create the window
	 */
	public PreferencesPrompt() {

		setWindowProperties();

		promptLabel = new Label(PROMPT_MESSAGE);
		promptLabel.setWidth(300);
		promptLabel.setMargin(5);

		buttonsLayout = new HLayout();
		buttonsLayout.setMembersMargin(5);
		buttonsLayout.setHeight(40);

		yesButton = new IButton(YES);
		noButton = new IButton(NO);

		noButton.setAutoFit(true);

		// Do not ask again layout
		saveLayout = new HLayout();
		saveLayout.setMembersMargin(15);

		saveForm = new DynamicForm();
		dontAskAgainCheckBox = new CheckboxItem();
		dontAskAgainCheckBox.setName(ASK_AGAIN);
		dontAskAgainCheckBox.setTitle(TITLE);
		dontAskAgainCheckBox.setRequired(false);

		saveForm.setFields(dontAskAgainCheckBox);

		// we want to get the checkbox value
		addClickHandlers();

		buttonsLayout.addMember(yesButton);
		buttonsLayout.addMember(noButton);
		buttonsLayout.setMargin(5);

		saveLayout.addMember(saveForm);
		saveLayout.setHeight(20);

		addItem(promptLabel);
		addItem(buttonsLayout);
		addItem(saveLayout);
	}

	private void setWindowProperties() {
		setIsModal(true);
		setShowModalMask(true);
		setAutoCenter(true);
		setAutoSize(true);
		setShowCloseButton(false);
		setShowMinimizeButton(false);
		setShowTitle(false);
		setAlign(Alignment.CENTER);
	}

	/**
	 * Events after clicking on buttons
	 */
	private void addClickHandlers() {

		noButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				saveCheckBoxValue();
				Services.getInstance().initializeServiceFactory(false);
				destroy();

				Macchiato.initApplicationPanel();
			}
		});

		yesButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				saveCheckBoxValue();
				new AllPreferences();
				destroy();
			}
		});
	}

	/**
	 * if the check box is checked, we consider the preferences as configured
	 */
	private void saveCheckBoxValue() {

		if (dontAskAgainCheckBox.getValueAsBoolean()) {
			Services.getInstance().getPreferencesService()
					.setPreferencesConfigured(true);
		}

	}

}
